package com.gitee.feizns.webtemplate.commons.utils;

import lombok.NonNull;

import java.util.Iterator;
import java.util.function.Predicate;

/**
 * @author feizns
 * @since 2019/5/13
 */
public abstract class ArrayUtils {

    /**
     * 查找第一个元素的下标
     * @param test
     * @param <T>
     * @return
     */
    public static <T> int findIndex(@NonNull Predicate<T> test, @NonNull T... array) {
        for (int i = 0; i < array.length; i++) {
            if ( test.test(array[i]) )
                return i;
        }
        return -1;
    }

    /**
     * 将数组包装成iterator
     * @param ele
     * @param <T>
     * @return
     */
    public static <T> Iterator<T> iterator(T... ele) {
        return new Iterator<T>() {

            int index = 0;

            @Override
            public boolean hasNext() {
                return index != ele.length;
            }

            @Override
            public T next() {
                return ele[index++];
            }

        };
    }

    /**
     * 将数组包装成功iterable
     * @param ele
     * @param <T>
     * @return
     */
    public static <T> Iterable<T> iterable(T... ele) {
        return () -> iterator(ele);
    }

    /**
     * 判断数组是否为空
     * @param arr
     * @return
     */
    public static final boolean isEmpty(Object... arr) {
        return arr == null || arr.length == 0;
    }

}
